package 栈.基础;

import java.util.Arrays;

public class MyStack {
    private int[] elem;
    private int usedSize;
    private static  final int Capacity = 10;
    MyStack(){
        this.elem = new int[Capacity];
    }

    Boolean isFull(){
        if(usedSize>=elem.length){
            return true;
        }
        return false;
    }

    public void push(int val){
        if(isFull()){
            //扩容
            elem = Arrays.copyOf(elem,elem.length+1);
        }
            elem[usedSize++] = val;
    }
    public Object  pop(){
        Object o = null;
        if(!isempty()){
//            usedSize表示下一个该放的位置
           o =  elem[usedSize-1];
           usedSize--;
        }else {
             System.out.println("栈为空");
             return null;
        }
        return o;
    }
    public Object peek(){
        Object o = null;
        if(!isempty()){
            o =  elem[usedSize-1];
        }else {
            System.out.println("栈为空");
            return null;
        }
        return o;
    }
    public Boolean isempty(){
        if(usedSize!=0){
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);;
        myStack.push(2);
        myStack.push(3);

        int x = (int) myStack.pop();
        System.out.println(x);

        int x1 = (int) myStack.peek();
        System.out.println(x1);

        int x2 = (int) myStack.peek();
        System.out.println(x2);

    }

}
